package com.zhongying.jf.util;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import javax.xml.bind.Element;
import java.math.BigDecimal;
import java.util.*;

/**
 * Created by tumin on 2017/5/26.
 */
public class ListUtil {
    /**
     * 去除list中的重复元素
     * @param list
     * @return
     */
    public static List removeDuplicate(List list) {
        for (int i = 0; i < list.size() - 1; i++) {
            for (int j = list.size() - 1; j > i; j--) {
                if (list.get(j).equals(list.get(i))) {
                    list.remove(j);
                }
            }
        }
        return list;
    }

    /**
     * @description list属性copy方法 copy源对象中属性到target对象中 相同属性相会copy 不同属性则不做处理 相同属性如果想忽略,传入filedNamesToSkip
     * @param sources 源对象
     * @param targetClass  目标对象的class对象
     * @param filedNamesToSkip 忽略的属性 (不copy忽略的属性)
     * @auther zengli
     * @create 2017/10/13
     */
    public static <E> List<E> copyTo(List<?> sources, Class<E> targetClass, String... filedNamesToSkip){
        if (sources==null||sources.size()==0) return Collections.emptyList();
        List<E> resultList = new ArrayList<E>(sources.size());
        try {
            for (Object source : sources) {
                E target = targetClass.newInstance();
                BeanUtils.copyProperties(source, target,filedNamesToSkip);
                resultList.add(target);
            }
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return resultList;
    }

    /**
     * 以指定大小分割源list 返回List<List<T>>
     * @param list  list源
     * @param size  分割的每个list中的元素个数
     * @auther zengli
     * @create 2017/10/15
     */
    public static <T> List<List<T>> subList(List<T> list, int size) {
        List<List<T>> result = new ArrayList<List<T>>();
        if(list==null||list.isEmpty()){
            return result;
        }
        int page = (list.size() - 1) / size + 1;
        for (int i = 0; i < page; i++) {
            int fromIndex = i * size;
            int toIndex = i == page - 1 ? list.size() : (i + 1) * size;
            result.add(list.subList(fromIndex, toIndex));
        }
        return result;
    }

    /**
     * 字符串指定分隔符转换成list
     * @param source 字符串 源
     * @param seperator 分隔符
     * @auther zengli
     * @create 2017/10/15
     */
    public static List<String> strToList(String source,String seperator){
        List<String> list=new ArrayList<String>();
        try {
            if(StringUtils.isNotBlank(source) && StringUtils.isNotBlank(seperator) ){
                String[] strArray = source.split(seperator);
                for(String str:strArray){
                    list.add(str);
                }
            }
        } catch (Exception e) {
        }
        return list;
    }

    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(1,2,3);
        Map<String, String> treemap = new HashMap<>();



    }
}
